package mdt.gui;

import mdt.figure.FigMessage;
import mdt.figure.FigMessage.ARROW_TYPE;
import net.miginfocom.swing.MigLayout;
import org.tigris.gef.presentation.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by lambert on 2016/7/23.
 */
public class EdgeArrowAttributePanel extends AttributePanel{

	JComboBox startShapeType;
	JComboBox endShapeType;


	public EdgeArrowAttributePanel() {
		addSeparator("Source Arrow Shape");

		JLabel shape = new JLabel("Shape");
		add(shape, "gap para");

		startShapeType = new JComboBox(getArrowTypes());
		startShapeType.setSelectedItem(getSrcArrowType());
		startShapeType.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				arrowChanged(e);
			}
		});
		add(startShapeType, "grow,wrap");

		add(new JLabel("Size"), "gap para");
		JTextField jftSrcSize = new JTextField(6);
		add(jftSrcSize, "grow, wrap");

		addSeparator("Destination Arrow Shape");

		JLabel endShape = new JLabel("Shape");
		add(endShape, "gap para");

		endShapeType = new JComboBox(getArrowTypes());
		endShapeType.setSelectedItem(getDstArrowType());
		endShapeType.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				arrowChanged(e);
			}
		});
		add(endShapeType, "grow,wrap");

		add(new JLabel("Size"), "gap para");
		JTextField jftDstSize = new JTextField(6);
		add(jftDstSize, "grow, wrap");
	}

	public ARROW_TYPE[] getArrowTypes() {
		return new ARROW_TYPE[] {
				ARROW_TYPE.NONE,
				ARROW_TYPE.HALF_TRIANGLE,
				ARROW_TYPE.TRIANGLE,
				ARROW_TYPE.QUALIFIER,
				ARROW_TYPE.DIAMOND,
				ARROW_TYPE.GREATER
		};
	}


	protected void initLayout() {
		setLayout(new MigLayout("", "[]10[fill,grow]", ""));
	}

	private void arrowChanged(ActionEvent e) {
		boolean startPoint = e.getSource() == startShapeType;
		ArrowHead ah = FigMessage.createArrowHead((ARROW_TYPE)(startPoint ? startShapeType : endShapeType).getSelectedItem());
		for (Fig fig : figs) {
			if (startPoint) {
				((FigMessage)fig).setSourceArrowHead(ah);
			} else {
				((FigMessage)fig).setDestArrowHead(ah);
			}
			fig.damage();
		}

	}

	public ARROW_TYPE getSrcArrowType() {
		return getArrowType(((FigEdge)firstFig).getSourceArrowHead());
	}

	public ARROW_TYPE getDstArrowType() {
		return getArrowType(((FigEdge)firstFig).getDestArrowHead());
	}

	private ARROW_TYPE getArrowType(ArrowHead ah) {
		Class cls = ah.getClass();
		if (cls.equals(ArrowHeadNone.class))
			return ARROW_TYPE.NONE;
		else if (cls.equals(ArrowHeadTriangle.class))
			return ARROW_TYPE.TRIANGLE;
		else if (cls.equals(ArrowHeadHalfTriangle.class))
			return ARROW_TYPE.HALF_TRIANGLE;
		else if (cls.equals(ArrowHeadDiamond.class))
			return ARROW_TYPE.DIAMOND;
		else if (cls.equals(ArrowHeadGreater.class))
			return ARROW_TYPE.GREATER;
		else if (cls.equals(ArrowHeadQualifier.class))
			return ARROW_TYPE.QUALIFIER;
		else
			return null;

	}

}
